package dao

import (
	"frontend-server/db"
	model "frontend-server/model/gv_im"
)

// 创建房间
func CreateGvImRoom(room model.GvImRoom) (model.GvImRoom, error) {
	db := db.GetImDB()
	// rm := model.GvImRoom{}
	err := db.Table("gv_im_room").Create(&room).Error
	// fmt.Println(err, rm)
	return room, err
}

// 根据房间创建者id和群id和群号获取房间id
func GetGvImRoomByUserIDRoomIDNumber(userID, roomID uint, number string) (model.GvImRoom, error) {
	db := db.GetImDB()
	room := model.GvImRoom{}
	err := db.Table("gv_im_room").Where(&model.GvImRoom{
		ID:     roomID,
		Number: number,
		UserID: userID,
	}).First(&room).Error
	return room, err
}

// 根据房间 id 删除房间
func DeleteGvImRoomByID(roomID uint) error {
	db := db.GetImDB()
	err := db.Table("gv_im_room").Unscoped().Delete(&model.GvImRoom{
		ID: roomID,
	}, "id = ?", roomID).Error
	return err
}

// 根据用户关联列表查询房间列表
func GetGvImRoomByUserRooms(userRooms []model.GvImUserRoom) ([]model.GvImRoom, error) {
	db := db.GetImDB()
	rooms := make([]model.GvImRoom, 0)
	roomIDS := make([]uint, 0)
	for _, value := range userRooms {
		roomIDS = append(roomIDS, value.RoomID)
	}
	err := db.Table("gv_im_room").Find(&rooms, roomIDS).Error
	return rooms, err
}
